package com.ruoyi.system.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.QxChestnutAdoptModel;
import com.ruoyi.system.domain.QxChestnutAdoptModelHistory;
import com.ruoyi.system.domain.vo.ExportExcelHistoryModelListVO;
import com.ruoyi.system.domain.vo.ExportExcelModelListVO;
import com.ruoyi.system.mapper.QxChestnutAdoptModelHistoryMapper;
import com.ruoyi.system.service.IQxChestnutAdoptModelHistoryService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * 树苗认领记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-01
 */
@Service
public class QxChestnutAdoptModelHistoryServiceImpl implements IQxChestnutAdoptModelHistoryService {
	@Autowired
	private QxChestnutAdoptModelHistoryMapper qxChestnutAdoptModelHistoryMapper;

	/**
	 * 查询树苗认领记录
	 *
	 * @param treeId 树苗认领记录主键
	 * @return 树苗认领记录
	 */
	@Override
	public QxChestnutAdoptModelHistory selectQxChestnutAdoptModelHistoryByTreeId(Long treeId) {
		return qxChestnutAdoptModelHistoryMapper.selectQxChestnutAdoptModelHistoryByTreeId(treeId);
	}

	/**
	 * 查询树苗认领记录列表
	 *
	 * @param qxChestnutAdoptModelHistory 树苗认领记录
	 * @return 树苗认领记录
	 */
	@Override
	public List<QxChestnutAdoptModelHistory> selectQxChestnutAdoptModelHistoryList(
			QxChestnutAdoptModelHistory qxChestnutAdoptModelHistory) {
		List<QxChestnutAdoptModelHistory> list =  qxChestnutAdoptModelHistoryMapper.selectQxChestnutAdoptModelHistoryList(qxChestnutAdoptModelHistory);
		list.forEach(c -> {
			if (StringUtils.isNotEmpty(c.getSpringImages())) {
				List<String> springs = JSON.parseArray(c.getSpringImages(), String.class);
				c.setSpringImagesList(springs);
			}
			if (StringUtils.isNotEmpty(c.getAutumnImages())) {
				List<String> autumn = JSON.parseArray(c.getAutumnImages(), String.class);
				c.setAutumnImagesList(autumn);
			}
			if (StringUtils.isNotEmpty(c.getSummerImages())) {
				List<String> sum = JSON.parseArray(c.getSummerImages(), String.class);
				c.setSummerImagesList(sum);
			}
			if (StringUtils.isNotEmpty(c.getWinterImages())) {
				List<String> winter = JSON.parseArray(c.getWinterImages(), String.class);
				c.setWinterImagesList(winter);
			}

		});
		return list;
	}

	@Override
	public int insertQxChestnutAdoptModelHistory(QxChestnutAdoptModelHistory qxChestnutAdoptModelHistory) {
		return qxChestnutAdoptModelHistoryMapper.insertQxChestnutAdoptModelHistory(qxChestnutAdoptModelHistory);
	}

	@Override
	public void exportHistoryListToExcelService(HttpServletResponse response, QxChestnutAdoptModelHistory qxChestnutAdoptModelHistory) {
		{
			try {
				// 设置信息头，告诉浏览器内容为excel类型
				response.setHeader("content-Type", "application/vnd.ms-excel");
				// 设置下载名称
				response.setHeader("Content-Disposition",
						"attachment;filename=" + URLEncoder.encode("认养记录列表.xls", StandardCharsets.UTF_8.name()));
				// 字节流输出
				OutputStream out = response.getOutputStream();
				// 设置excel参数
				ExportParams params = new ExportParams();
				// 设置sheet名名称
				params.setSheetName("认养记录列表");
				// 设置标题
				params.setTitle("认养记录列表");
				List<ExportExcelHistoryModelListVO> exportList = new ArrayList<ExportExcelHistoryModelListVO>();
				List<QxChestnutAdoptModelHistory> list = this.selectQxChestnutAdoptModelHistoryList(qxChestnutAdoptModelHistory);
				ExportExcelHistoryModelListVO vo = null;
				for (QxChestnutAdoptModelHistory model: list) {
					vo = new ExportExcelHistoryModelListVO();
					BeanUtils.copyBeanProp(vo,model);
//					List<String> splist = vo.getSpringImagesList();
//					if(splist!=null){
//						vo.setSpringImage1(splist.size()>=1?buildImgPath(splist.get(0)):"");
//						vo.setSpringImage2(splist.size()>=2?buildImgPath(splist.get(1)):"");
//						vo.setSpringImage3(splist.size()>=3?buildImgPath(splist.get(2)):"");
//					}
//					List<String> smlist = vo.getSummerImagesList();
//					if(smlist!=null){
//						vo.setSummerImage1(smlist.size()>=1?buildImgPath(smlist.get(0)):"");
//						vo.setSummerImage2(smlist.size()>=2?buildImgPath(smlist.get(1)):"");
//						vo.setSummerImage3(smlist.size()>=3?buildImgPath(smlist.get(2)):"");
//					}
//					List<String> autumnImagesList = vo.getAutumnImagesList();
//					if(autumnImagesList!=null){
//						vo.setAutumnImage1(autumnImagesList.size()>=1?buildImgPath(autumnImagesList.get(0)):"");
//						vo.setAutumnImage2(autumnImagesList.size()>=2?buildImgPath(autumnImagesList.get(1)):"");
//						vo.setAutumnImage3(autumnImagesList.size()>=3?buildImgPath(autumnImagesList.get(2)):"");
//					}
//					List<String> winterImagesList= vo.getWinterImagesList();
//					if(winterImagesList!=null){
//						vo.setWinterImage1(winterImagesList.size()>=1?buildImgPath(winterImagesList.get(0)):"");
//						vo.setWinterImage2(winterImagesList.size()>=2?buildImgPath(winterImagesList.get(1)):"");
//						vo.setWinterImage3(winterImagesList.size()>=3?buildImgPath(winterImagesList.get(2)):"");
//					}
					if(model.getStatus()!=null){
						vo.setStatus(model.getStatus()==1?"领养申请审核不通过":"结束领养");
					}
					exportList.add(vo);
				}
				// 导入excel
				Workbook workbook = ExcelExportUtil.exportExcel(params, ExportExcelHistoryModelListVO.class, exportList);
				workbook.write(out);
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private String buildImgPath(String urlPath){
		String[] s = urlPath.split("/treeImages/");
		String fileAbPath = RuoYiConfig.getBLImagesPath() + s[1];
		return fileAbPath;
	}
}
